package au.com.iglootech.TaxTimeToolTip.ui.company;

import au.com.iglootech.TaxTimeToolTip.style.TaxTimeToolTipStyle;
import au.com.iglootech.component.IGTTfnField;

import com.vaadin.data.Item;
import com.vaadin.data.validator.EmailValidator;
import com.vaadin.data.validator.IntegerValidator;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.TextField;

public class CompanyFieldsFactory extends DefaultFieldFactory {
	@Override
	public Field createField(Item item, Object propertyId, Component uiContext) {
		Field f;
		if ("tfn".equals(propertyId)) {
			IGTTfnField tf = new IGTTfnField();
			tf.setRequired(true);
			tf.setCaption("TFN");
			tf.setRequiredError("Please enter TFN");
			tf.setWidth(TaxTimeToolTipStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new StringLengthValidator(
					"Last Name must be 3-50 characters", 3, 50, false));
			return tf;
		} 
		// Use the super class to create a suitable field base on the
		// property type.
		f = super.createField(item, propertyId, uiContext);

		if ("companyName".equals(propertyId)) {
			TextField tf = (TextField) f;
			tf.setRequired(true);
			tf.setRequiredError("Please enter the company name");
			tf.setNullRepresentation("");
			tf.setWidth(TaxTimeToolTipStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new StringLengthValidator(
					"First Name must be 3-25 characters", 3, 25, false));
		} else if ("contactNumber".equals(propertyId)) {
			TextField tf = (TextField) f;
			tf.setRequired(true);
			tf.setRequiredError("Please enter a contact number");
			tf.setNullRepresentation("");
			tf.setWidth(TaxTimeToolTipStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new StringLengthValidator(
					"Last Name must be 3-50 characters", 3, 50, false));
		} else if ("contactName".equals(propertyId)) {
			TextField tf = (TextField) f;
			tf.setRequired(true);
			tf.setRequiredError("Please enter a contact name");
			tf.setNullRepresentation("");
			tf.setWidth(TaxTimeToolTipStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new StringLengthValidator(
					"Last Name must be 3-50 characters", 3, 50, false));
		} else if ("address1".equals(propertyId)) {
			TextField tf = (TextField) f;
			tf.setCaption("Address");
			tf.setRequired(true);
			tf.setRequiredError("Please enter the address");
			tf.setNullRepresentation("");
			tf.setWidth(TaxTimeToolTipStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new StringLengthValidator(
					"Last Name must be 3-50 characters", 3, 50, false));
		} else if ("address2".equals(propertyId)) {
			TextField tf = (TextField) f;
			tf.setCaption("");
			tf.setRequired(true);
			tf.setRequiredError("Please enter the address");
			tf.setNullRepresentation("");
			tf.setWidth(TaxTimeToolTipStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new StringLengthValidator(
					"Last Name must be 3-50 characters", 3, 50, false));
		} else if ("address3".equals(propertyId)) {
			TextField tf = (TextField) f;
			tf.setCaption("");
			tf.setRequired(true);
			tf.setRequiredError("Please enter the address");
			tf.setNullRepresentation("");
			tf.setWidth(TaxTimeToolTipStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new StringLengthValidator(
					"Last Name must be 3-50 characters", 3, 50, false));
		} else if ("email".equals(propertyId)) {
			TextField tf = (TextField) f;
			tf.setRequired(true);
			tf.setRequiredError("Please enter a Last Name");
			tf.setNullRepresentation("");
			tf.setWidth(TaxTimeToolTipStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new EmailValidator("Your input is not validate email address."));
		}

		return f;
	}

}
